<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2021/4/26
 * Time: 17:48
 */

namespace app\api\controller\v1;


use app\common\model\CaseModel;
use app\common\model\Cate;
use app\common\model\Article;
use app\common\model\Comment;
use app\common\model\Member;


class Wenda extends BaseController
{
    public function wendalist()
    {
        $page = input('page/d', 1);
        $order = input('order', 'sort');
        $field = input('field', 'desc');
        $order = "$order $field";

        $cate = Cate::where(['status' => 1, 'type' => 4])->select()->toArray();
        $cate_id = array_column($cate, 'id');
        $cate = getTree($cate);

        $id = input('cate_id');

        if (!empty($id)) {
            $where = ['cate_id' => $id];
            $cate_this = Cate::find($id);
        } else {
            $where[] = ['cate_id', 'in', $cate_id];  //根据子级查询数据
            $cate_this = ['id' => 0, 'name' => '实时问答'];
        }

        $data = Article::where(['status' => 1])
            ->where($where)
            ->field('id,title,image,content,zan_num,collect_num,comment_num,created_at')
            ->order($order)
            ->limit(($page - 1) * 6, 6)
            ->select()->toArray();
        if ($data) {
            $data = changeImage($data, 'image');
            $data = changeTime($data, 'created_at');
            $data = delHtml($data, 'content');
        }
        $maxpage = Article::where($where)
            ->where('status', 1)
            ->field('id')
            ->count();
        $maxpage = ceil($maxpage / 7);
        return sucessMsg(200, '获取成功', [
            'cate' => $cate,
            'data' => $data,
            'cate_this' => $cate_this,
            'maxpage' => $maxpage,
        ]);
    }

    public function search()
    {
        $keyword = input('keyword');
        if (!$keyword) {
            return errMsg(400, '请输入关键词');
        }
        $cate = Cate::where(['status' => 1, 'type' => 4])->select()->toArray();
        $cate_id = array_column($cate, 'id');
        $data = Article::where('title', 'like', "%$keyword%")
            ->where('cate_id', 'in', $cate_id)
            ->select();
        if ($data) {
            $data = changeImage($data, 'image');
            $data = changeTime($data, 'created_at');
            $data = delHtml($data, 'content');
        }
        return sucessMsg(200, '获取成功', $data);
    }

    public function content()
    {
        $id = input('id');
        if (!$id) {
            return errMsg(400, '请传入参数');
        }
        $data = Article::find($id);
        //$cate = Cate::find($data->cate_id);
        $data->image = env('app.yuming') . $data->image;
        $comment = Comment::where('article_id', $data->id)
            ->order('created_at asc')
            ->field('id,member_id,pid,comment,created_at')
            ->select()->toArray();
        $member_id = array_column($comment, 'member_id');

        $member = Member::where('id', 'in', array_unique($member_id))
            ->field('id,account,image')
            ->select()
            ->toArray();
        $member = changeImage($member, 'image');
        $member_id = array_column($member, 'id');  //键值
        $member = array_combine($member_id, $member);
        //dd($member);

        $comment_id = array_column($comment, 'id');  //键值
        $comment = array_combine($comment_id, $comment);
        foreach ($comment as &$v) {
            $v['benren'] = $member[$v['member_id']];
            if ($v['pid'] != 0) {
                $v['huifuren'] = $member[$comment[$v['pid']]['member_id']];
            }
        }
        //$a = getTree($comment);
        $list = $comment;
        $temp = [];
        foreach ($list as $key => $comment) {
            //parent_id为0的调用getSubtree方法进行递归循环
            if ($comment['pid'] == 0) {
                unset($list[$key]);
                $comment['childrenList'] = $this->getSubTree($list, $comment['id']);
                $temp[] = $comment;
            }
        }
        return sucessMsg(200, '获取成功', ['data' => $data, 'comment' => $temp]);
    }

    public function getSubTree($list, $pid)
    {
        $temp = [];
        foreach ($list as $key => $comment) {
            if ($comment['pid'] == $pid) {
                $temp[] = $comment;
                unset($list[$key]);
                $tempList = $this->getSubTree($list, $comment['id']);
                $temp = array_merge($temp, $tempList);
            }
        }
        return $temp;
    }
}